package cn.gcjxsc.service.merchant;

import cn.gcjxsc.entity.merchant.Account;
import cn.gcjxsc.repository.merchant.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author pumbaa
 * @since 1.0
 */
@Service("merchantAccountService1")
public class AccountService {
    @Autowired
    private AccountRepository accountRepository;

    /**
     * 根据商户id查询账户信息
     * @param id 商户id
     * @return 返回商户账户信息
     */
    public Account findByMerchantId(Integer id) {
        Account account = accountRepository.findByMerchantId(id);
        if (null == account) {
            account = new Account();
            account.setMerchantId(id);
            account.setCoin(BigDecimal.ZERO);
            account.setCash(BigDecimal.ZERO);
        }
        return account;
    }

    /**
     * 保存或更新商户账户信息
     * @param accounts 账户列表
     * @return 返回最新状态的账户信息
     */
    @Transactional
    public List<Account> saveOrUpdate(List<Account> accounts) {
        return accountRepository.save(accounts);
    }

    /**
     * 保存或更新商户账户信息
     * @param account 账户
     * @return 返回最新状态的账户信息
     */
    @Transactional
    public Account saveOrUpdate(Account account) {
        return accountRepository.save(account);
    }
}
